package com.frxuexi.park2304.news

import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.http.ApiService
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.mvi.RequestType
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(val apiService: ApiService):BaseViewModel<NewsIntent>() {
    override fun handleIntent(it: NewsIntent) {
        when(it){
            is NewsIntent.getNews->httpRequest(apiService.getNews())
            is NewsIntent.revokeNews->httpRequest(apiService.revokeNews(it.id),RequestType.UPDATE)
            is NewsIntent.getNewsByCon->httpRequest(apiService.getNewsByCon(it.con),RequestType.ITEM)
            is NewsIntent.upload->httpRequest(apiService.upload(toMultipartBody(it.name,it.path).parts,"news",SPUtils.getInstance().getInt(Const.UID).toString()),RequestType.UPLOAD)
            is NewsIntent.addNews->httpRequest(apiService.addNews(toBody(it.item)),RequestType.ADD)
            is NewsIntent.deleteNews->httpRequest(apiService.deleteNews(it.id),RequestType.DELETE)
            is NewsIntent.getNewsById->httpRequest(apiService.getNews(it.id))
            is NewsIntent.updateNews->httpRequest(apiService.updateNews(it.id,toBody(it.item)),RequestType.UPDATE)
        }
    }
}